
window.$Page = new Page({
    properties : {
        bIsAuthDataInit : false,
        bIsOptionalOrderDataInit : false,
        aAuthResult : [],
        iAuthStatus : 1,
        oSubscribeParameters : null,
        iTimeStamp : "",
        bIsFirstLogin : true,
        iBean : 0,
        iExp : 0,
        iDoLoadingStatus : $URL.getQueryStringByName("needLoading") == "0" ? 0 : 1,
        strAuthProductId : '',//鉴权成功的Id
        oSubscribeParameters : {
            "token":"",
            "currentTime":"",
            "productId": $Config.VALUE_ADDED_PRODUCT_ID,
        }
    },
    initPage : function(){
        //这里暂时先包扎一下，存储页面cookie的方案可能要调整一下

        if($Cookie.getOneAll("SharedDirectoryPageCookie")){
            $Cookie.delOneAll("SharedDirectoryPageCookie","/");
        }

        $Page.getGodMode();
    
        $Page.initEPGLoginParameters();
        $Page.getToken();
        //$Page.showLog();
        document.onkeydown = function(e){
            $KeyHandler.onKeyDown(e);
        }
        $KeyHandler.initPageKey($Page.onPageKeyDown, $Page.onPageKeyUp);

        $Page.run();
        
    },
    run : function(){
        //订购成功返回的探针的处理
        $Page.auth();
        $HB.getOptionalOrderInfo(function(){
            $Page.bIsOptionalOrderDataInit = true;
            $Page.checkOperationReady();
        });
    },
    /**
     * 鉴权
     * @param {*} strMediaId 
     * @param {*} strEpisodeId 
     */
    auth : function(){
        var fnDone = function(strProductId){
            try {
                //登录的探针
                $Probe.Login({
                    productId : strProductId,
                    from : {
                        type : "common"
                    }
                });
            } catch (error) {
                
            }
            $Page.bIsAuthDataInit = true;
            $Page.checkOperationReady();
        }
        try {
            $HB.Auth(null,null,$Utility.timeoutify(function(iAuthStatus,strProductId){
                $Page.iAuthStatus = iAuthStatus || 0;
                fnDone(strProductId||"");
                
            },2000,function(){
                fnDone("");
               
            },"auth"));
        } catch (error) {
            fnDone("");
        }
       
    },
    checkOperationReady : function(){
        if($Page.bIsAuthDataInit && $Page.bIsOptionalOrderDataInit){
            $Cookie.set("isAuthenticated", $Page.iAuthStatus);
            //$Log.info("enter checkOperationReady 55"+document.cookie);
            //$Log.info("document.location.href::::"+document.location.href);
            $Cookie.del("domain");
            $Cookie.set("domain",decodeURIComponent($URL.getQueryStringByName("ReturnURL")).split('?')[0]);
            //$Log.warn(document.cookie);
            var strTarget = $Cookie.getOneAll("target")&&$Cookie.getOneAll("target")!=""&&$Cookie.getOneAll("target").replace(/"/g,"")!="" ? decodeURIComponent($Cookie.getOneAll("target")) : $Config.PAGES["index"];
            var aTarget = strTarget.split("?");
            var strTargetURL= ($Config.PAGES[aTarget[0]] || aTarget[0])+(aTarget[1] ? "?"+(aTarget[1] || "") : "");//这里的获取方式要再考虑
            var strRefer = encodeURIComponent($Cookie.getOneAll("referURL"));
            $Cookie.delOneAll('referURL','/clutch');
            $Cookie.delOneAll('referURL','/eduHall');
            $Cookie.delOneAll('target','/clutch');
            $Cookie.delOneAll('target','/eduHall');
            var strDestURL = "loading.html?targetURL=" + encodeURIComponent(strTargetURL) + "&referURL=" + strRefer;
            //单片链接不要loading
            if(0 == strTargetURL.indexOf("wherry")){
                strDestURL = strTargetURL + ( -1!=strTargetURL.indexOf("?") ? "&" : "?") +  "referURL=" + strRefer;
            }
            window.location.href=strDestURL;
        }
       
    },
    /**
     * 初始化EPG信息
     */
    initEPGLoginParameters : function () {
        try {
            var strServerPath = decodeURIComponent($Cookie.getOneAll('sessionServer'));
            var strPartner = $Cookie.getOneAll('partner');
            var strAPIToken = $Cookie.getOneAll('token');
            var strPlatformAccount = $Cookie.getOneAll('platformUserId');
            var strUserId = $Cookie.getOneAll('ossUserId') || $Cookie.getOneAll('OssUserId');
            var strSelfUserId=decodeURIComponent($Cookie.getOneAll('userId') || $Cookie.getOneAll('UserId'));
            var strAppId = decodeURIComponent($Cookie.getOneAll('appId'));
    
    
            //把不要的删掉
            $Cookie.delOneAll('appId',"/clutch");
            $Cookie.delOneAll('partner',"/clutch");
            $Cookie.delOneAll('partner','/');
            $Cookie.delAll();
            $Cookie.delOneAll('token',"/clutch");
            $Cookie.delOneAll('sessionServer',"/clutch");
            $Cookie.delOneAll('ossUserId',"/clutch");
            $Cookie.delOneAll('userId',"/clutch");
            $Cookie.delOneAll('userProfile',"/clutch");
            $Cookie.delOneAll('userGroup',"/clutch");
            $Cookie.delOneAll('platformUserId',"/clutch");



            if (strAppId) {
                $Cookie.set('appId', strAppId);
            }
            if (strAPIToken) {
                $Cookie.set('apiToken', strAPIToken);
            }
            if (strPlatformAccount) {
                $Cookie.set('platformAccount', strPlatformAccount);
            }
            if(strSelfUserId){
                $Cookie.set('selfUserId',strSelfUserId);
                $Cookie.set('userId', strUserId||strSelfUserId);
            }
            if (strPartner) {
                strPartner = strPartner.toLowerCase();
                $Cookie.set('partner', strPartner);
               
                if (-1 != strPartner.indexOf('huawei')) {
                    strPartner = 'huawei';
                } else {
                    strPartner = 'zte';
                }
                $Constant.OPERATOR_PLATFORM = strPartner; //有可能之前没拿到
                $Cookie.set('operatorPlatform', strPartner);
            }
            if (strServerPath) {
                $Cookie.set('serverPath', strServerPath);
            }
        } catch (error) {
            $Log.error("initEPGLoginParameters error==>"+error);
        }
        
    }
    
});

